package com.knife.hospital.service.QueryDoctor;

import com.knife.hospital.config.BaseException;
import com.knife.hospital.constant.ResponseCode;
import com.knife.hospital.entity.ArrangementPO;
import com.knife.hospital.entity.DoctorPO;
import com.knife.hospital.entity.OrderPO;
import com.knife.hospital.mapper.*;
import com.knife.hospital.model.OrderDoctor.DocOrderRequest;
import com.knife.hospital.model.OrderDoctor.DocOrderResponse;
import com.knife.hospital.model.OrderDoctor.DoctorInfoResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class QueryDoctorInfoServiceImpl implements QueryDoctorInfoService {

    @Resource
    private DoctorPOMapper doctorPOMapper;
    @Resource
    private OrderPOMapper orderPOMapper;
    @Resource
    private UserPOMapper userPOMapper;
    @Resource
    private ArrangementPOMapper arrangementPOMapper;
    @Resource
    private DepartmentPOMapper departmentPOMapper;

    @Override
    public DoctorInfoResponse getDoctorInfo(int id) {
        DoctorPO doctorPO = doctorPOMapper.selectByPrimaryKey(id);
        DoctorInfoResponse doctorInfoResponse = new DoctorInfoResponse();

        // todo 这部分建议搞成一个Util类对象进行转换，即将代码抽出来
        doctorInfoResponse.setAge(doctorPO.getAge());
        doctorInfoResponse.setGender(doctorPO.getGender());
        doctorInfoResponse.setName(doctorPO.getName());
        doctorInfoResponse.setPhoto(doctorPO.getPhoto());
        doctorInfoResponse.setProfile(doctorPO.getProfile());
        doctorInfoResponse.setReserved(doctorPO.getReserved());
        doctorInfoResponse.setId(doctorPO.getId());
        doctorInfoResponse.setDepartmentName(departmentPOMapper.selectByPrimaryKey(doctorPO.getDepartmentId()).getName());

        return doctorInfoResponse;
    }

    @Override
    public List<OrderPO> getUserOrdersByPatientId(int id) {
        // todo 感觉这名字起的有问题啊
        // todo 这不是根据医生的id查orders吗，建议名字变成getOrdersByDoctorId
        return orderPOMapper.getDoctorOrdersByDoctorID(id);
    }

    @Override
    public List<DocOrderResponse> getOrderByDate(DocOrderRequest docOrderRequest) {
        int id = docOrderRequest.getDoctorId();
        Date date = docOrderRequest.getDate();
        List<OrderPO> orderPOS = orderPOMapper.getOrderByDate(id, date);
        List<DocOrderResponse> docOrderResponses = new ArrayList<>();
        for (OrderPO orderPO : orderPOS) {
            DocOrderResponse docOrderResponse = new DocOrderResponse();
            try {
                // todo 同上，这部分代码应该抽出来，生成一个Util类
                docOrderResponse.setId(orderPO.getId());
                docOrderResponse.setPatient_name(userPOMapper.selectByPrimaryKey(orderPO.getPatientId()).getNickname());
                docOrderResponse.setDate(orderPO.getDate());
                docOrderResponse.setTimeQuantum(orderPO.getTimeQuantum());
                docOrderResponse.setStatement(orderPO.getStatement());
            } catch (Exception e) {
                throw new BaseException(ResponseCode.RESOURCES_NOT_EXIST);
            }
            docOrderResponses.add(docOrderResponse);
        }
        return docOrderResponses;
    }

    @Override
    public List<ArrangementPO> getArrangement(DocOrderRequest docOrderRequest) {
        return arrangementPOMapper.getArrangement(docOrderRequest.getDoctorId(), docOrderRequest.getDate());
    }
}
